<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CartController extends Controller
{
   /*
        购物车添加操作
   */
        public function insert(Request $request)
        {	
          //提取商品id
          $gid = $request->input('gid');
          
          $id=1;
          //检测商品是否存在于购物车中
          if($this->check($id,$gid)){
            //修改数量
            $cart=DB::table('shop_cart')->where('id',$id)->get();
            foreach ($cart as $k => $v) {
                          //如果这个用户 有相同的物品在购物车
                          if($v->id==$id && $v->gid==$gid){
                            $cid=$v->cid;
                            DB::table('shop_cart')->where('cid',$cid)->increment('num', 1);
                            // dd($cid);
                          }
                        }
            }else{
            
              $res=DB::table('shop_cart')->insert(
                  [
                    'id' => $id, 
                    'gid' =>$gid,
                    'num' =>1
                  
                  ]
              );
              // dd($cart);
          }
          // dd($cart);
          //跳转到列表页
          return redirect('/cart/index');
        }

        //定义方法来检测商品是否存在购物车中
          private function check($id,$gid)
          {
            // $gid = $request->input('gid');
            
            //提取购物车信息
            $cart=DB::table('shop_cart')->where('id',$id)->get();
            //检查该用户在购物车有无商品
            if(empty($cart)){
              return false;
            }
            // dd($cart);
            //遍历购物车
            foreach ($cart as $k => $v) {
              //如果这个用户 有相同的物品在购物车
              // dd($v);
              if($v->id==$id && $v->gid==$gid){
                return true;
              }
            }

            //如果都不存在
            return false;
          }

        public function index(Request $request)
        { 
          
          //获取购物车数据 购车表和商品表联查获取数据
          
          $carts = DB::table('shop_cart')
                      ->leftJoin('shop_goods', 'shop_goods.gid', '=', 'shop_cart.gid')
                      ->get();
         
          // dd($carts);

          //解析模板 根目录为resources下的views
          return view('Home.Cart.index',['carts'=>$carts]);
          
        }
        // 修改购物车物品数量
        public function update(Request $request)
        {
          //提取要修改数据 cid num
          $data=$request->except(['_token']);
          //修改数据格式
          $cart=[];
          foreach ($data['cid'] as $k => $v) {
            $tmp['cid']=$v;
            $tmp['num']=$data['num'][$k];
            $cart[]=$tmp;
          }

          //修改数据库
          foreach ($cart as $k => $v) {
           $res=DB::table('shop_cart')
                        ->where('cid', $v['cid'])
                        ->update(['num' => $v['num']]);
          }
          //跳转页面
          return redirect('/home/order');
        }

        /*
            封装方法 进行ajax删除操作
        */
        public function delete(Request $request)
        {
            // echo 'delete';
             $cid = $request->input('cid');
            // dd($cid);
            //删除用户
            // $res = DB::table('shop_goods')->delete($gid);
            $res = DB::delete("delete from shop_cart where cid=".$cid);
            if($res){
                //成功 到列表页
                return redirect('/cart/index');
            }else{
                return back();
            }
        }

        //清除购物车
        public function clear(Request $request)
        {
          $request->session()->forget('cart');
        }
}
